<?php
/**
 * WankenBerry
 * By Levi Senft
 * November 19, 2007
 *
 * --
 * Copyright (c) 2007 Levi Senft
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * --
 *
 * @package WankenBerry.php
 * @author Levi Senft <lsenft@gmail.com>
 * @copyright 2007 Levi Senft <lsenft@gmail.com> (wankenberry.php)
 * @license http://opensource.org/licenses/mit-license.php MIT License
 * @version 0.0.1 (2007-11-19)
 * @link http://code.google.com/p/wankenberry/
 */

require_once("jsmin-1.1.0.php");

class WankenBerry {

	// The build location for your files
	const BUILDPATH = "_build/";
	
	// The build location and filename for your style sheet
	const CSS_FILE = "_build/stylesheet.css";
	
	// The build location and filename for your javascript
	const JS_FILE = "_build/javascript.js";
	
	// Password to force a rebuild
	const FORCE_CODE = "doitbitch";
	
	// Flip this if you want to see how it looks without concatenating
	const CAT = true;
	
	// A private member to strip CSS of comments and whitespace
	private function minifycss($css) {
		// Compress whitespace.
		$css = preg_replace('/\s+/', ' ', $css);
	
		// Remove comments.
		$css = preg_replace('/\/\*.*?\*\//', '', $css);
	
		// Return minified and trimmed css
		return trim($css);
 	}
 	
 	private function getfilename($filePath) {
	 	
	 	// split directories and file name
	 	$filePathTokens = split('/', $filePath);
	 	
	 	// the file name should be the last token
		$fileName = $filePathTokens[count($filePathTokens)-1];
		
		$fileNameTokens = split('\.', $fileName);
		
		// put it all back together in a slightly stupid way... (will mangle names with muliple periods)
		$fileName = $fileNameTokens[0] . "-wbj." . $fileNameTokens[count($fileNameTokens)-1];
		
		return self::BUILDPATH . $fileName;
 	}


	// A public member to compile your css.  Pass it an array of file paths.
	public static function includecss ($cssFiles) {
		
		if ((isset($_REQUEST['force']) && $_REQUEST['force'] == self::FORCE_CODE) && file_exists(self::CSS_FILE)) {
			unlink(self::CSS_FILE);
		}
	
		// Does the stylesheet exist?
		if (!file_exists(self::CSS_FILE)) {
			// it doesn't, lets build it		
	
			// open the output file for writing
			$buildFile = fopen(self::CSS_FILE,"w");
			
			foreach ($cssFiles as $fileName) {
				$file = fopen($fileName,"r");
				
				// Send rest of the file to the output buffer
				$buffer = fread($file, filesize($fileName));
				$buffer = self::minifycss($buffer);
				fclose($file);
				
				// We're cat'ing, write out a single file
				if (self::CAT) {
					fwrite($buildFile, $buffer); 
					
					// I just want a hard return between files, just in case
					// Also one line # per file
					fwrite($buildFile, "\n");
				}
				else {

					$tmpBuildFile = fopen(self::getfilename($fileName) ,"w");
					
					fwrite($tmpBuildFile, $buffer); 
					fclose($tmpBuildFile);
					 
					echo "<link rel=\"stylesheet\" href=\"" . self::getfilename($fileName) . "\">\n";
				}
			}
			
			fclose($buildFile);
		}
		
		// Write out the single script tag for hour cat'd files
		if (self::CAT) {
			echo "<link rel=\"stylesheet\" href=\"" . self::CSS_FILE .  "\">\n";
		}
	}
	
	// A public member to compile your js.  Pass it an array of file paths.
	public static function includejs ($jsFiles) {
		
		if ((isset($_REQUEST['force']) && $_REQUEST['force'] == self::FORCE_CODE) && file_exists(self::JS_FILE)) {
			unlink( self::JS_FILE );
		}
		
		// Does the javascript exist?
		if (!file_exists(self::JS_FILE)) {
			// it doesn't, lets build it
			
			// open the output file for writing
			$buildFile = fopen( self::JS_FILE ,"w");
			
			foreach ($jsFiles as $fileName) {
	
				// Outsource javascript minification to JSMin.  Thank you Mr. Crockford.
				$buffer =  JSMin::minify(file_get_contents($fileName));
				
				// We're cat'ing, write out a single file
				if (self::CAT) {
					
					fwrite($buildFile, $buffer); 
				
					// I just want a hard return between files, just in case
					// Also one line # per file
					fwrite($buildFile, "\n");
				}
				else {
					$tmpBuildFile = fopen(self::getfilename($fileName) ,"w");
					
					fwrite($tmpBuildFile, $buffer); 
					fclose($tmpBuildFile);
					
					echo "<script type=\"application/javascript\" src=\"" . self::getfilename($fileName) ."\"></script>\n";
				}
	
			}
			
			fclose($buildFile);
		}
		
		// Write out the single script tag for hour cat'd files
		if (self::CAT) {
			echo "<script type=\"application/javascript\" src=\"". self::JS_FILE ."\"></script>\n";
		}
	}

}

?>